package com.woniuxy.lab.woniu_lab_boss.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class CorsConfig implements WebMvcConfigurer {

//    @Override
//    public void addCorsMappings(CorsRegistry registry){
//        registry.addMapping("/**") //拦截所有请求
//                .allowedOriginPatterns("*") //可跨域的域名 可以为*
//                .allowCredentials(true)
//                .allowedMethods("*") //允许跨域的方法，可以单独配置
//                .allowedHeaders("*"); //允许跨域的请求头，可以单独配置
//    }

    @Bean
    public CorsFilter getCorsFilter() {
        UrlBasedCorsConfigurationSource cfs = new UrlBasedCorsConfigurationSource();
        CorsConfiguration cors = new CorsConfiguration();
        cors.addAllowedOriginPattern("*");
        cors.addAllowedMethod("*");
        cors.setAllowCredentials(true);
        cors.addAllowedHeader("*");
        cfs.registerCorsConfiguration("/**", cors);
        return new CorsFilter(cfs);
    }

}
